package cn.javadog.ss.spring.jdbc;

import javax.sql.DataSource;

import java.util.List;
import java.util.Map;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

/**
 * @author 余勇
 * @date 2019年11月25日 17:59:00
 */
@Configuration
public class JdbcBootstrap {

	public static void main(String[] args) {
		AnnotationConfigApplicationContext applicationContext =
			new AnnotationConfigApplicationContext(JdbcBootstrap.class);

		JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
		List<Map<String, Object>> maps = jdbcTemplate.queryForList("SELECT * FROM t_like");
		System.out.println(maps);
	}


	@Bean
	public DataSource jdbcDataSource() {
		final String URL = "jdbc:mysql://localhost:3306/mysql_learn?useSSL=true";
		final String DRIVER = "com.mysql.cj.jdbc.Driver";
		final String USERNAME = "root";
		final String PASSWORD = "root";

		DriverManagerDataSource dataSource = new DriverManagerDataSource(URL,USERNAME,PASSWORD);
		dataSource.setDriverClassName(DRIVER);

		return dataSource;
	}

	@Bean
	public JdbcTemplate jdbcTemplate(DataSource dataSource){
		return new JdbcTemplate(dataSource);
	}

}
